package com.hxd.otc.module.wallet.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseAdapter;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.module.wallet.bean.WalletCurrencyBean;
import com.hxd.otc.utils.StringUtil;
import com.hxd.otc.utils.UIUtils;
import com.hxd.otc.widget.BlocksToast;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lzy on 2018/9/21 0021.
 * description ${钱包币种Adapter}
 */
public class WalletCurrencyAdapter extends BaseAdapter<RecyclerView.ViewHolder, WalletCurrencyBean> {

    private int pos;

    public WalletCurrencyAdapter(Context context, int pos) {
        super(context);
        this.pos = pos;
    }

    @Override
    protected void onBindHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        onBindWalletCurrencyHolder((WalletCurrencyHolder) holder, position);
    }

    private void onBindWalletCurrencyHolder(@NonNull final WalletCurrencyHolder holder, final int position) {
        final WalletCurrencyBean item = getItem(position);
        holder.tvCoinLogo.setText(item.getName());

        holder.tvAvailableBalance.setText(StringUtil.formatNumberRetainZero(item.volume, 8, false));
        holder.tvFreezeBalance.setText(StringUtil.formatNumberRetainZero((item.otcAdvertVolume + item.otcLockVolume), 8, false));
        switch (pos) {
            case 0:
                holder.tvEnter.setText(R.string.wallet_into);
                holder.tvOut.setVisibility(View.GONE);
                if (UserHelper.isSilver()) {
                    holder.tvFeeReserve.setVisibility(View.VISIBLE);
                    holder.tvFeeReserveLable.setVisibility(View.VISIBLE);
                    holder.tvFeeReserve.setText(StringUtil.format8Number(item.bailVolume));
                } else {
                    holder.tvFeeReserve.setVisibility(View.GONE);
                    holder.tvFeeReserveLable.setVisibility(View.GONE);
                }

                break;
            case 1:
                holder.tvOut.setVisibility(View.VISIBLE);
                holder.tvEnter.setText(R.string.wallet_recharge);
                holder.tvOut.setText(R.string.wallet_withdraw);
                holder.tvFeeReserve.setVisibility(View.GONE);
                holder.tvFeeReserveLable.setVisibility(View.GONE);
                switch (item.tokenStatus) {
                    case 9:
                        holder.tvOut.setBackgroundDrawable(UIUtils.getContext().getResources().getDrawable(R.drawable.shape_wallet_currency_no_use));
                        holder.tvOut.setTextColor(UIUtils.getContext().getResources().getColor(R.color.colorWhite));

                        holder.tvEnter.setBackgroundDrawable(UIUtils.getContext().getResources().getDrawable(R.drawable.shape_wallet_currency_no_use));
                        holder.tvEnter.setTextColor(UIUtils.getContext().getResources().getColor(R.color.colorWhite));
                        break;
                    case 1:
                        holder.tvOut.setBackgroundDrawable(UIUtils.getContext().getResources().getDrawable(R.drawable.selector_wallet_btn_yellow));
                        holder.tvOut.setTextColor(UIUtils.getContext().getResources().getColor(R.color.color_text1));

                        holder.tvEnter.setBackgroundDrawable(UIUtils.getContext().getResources().getDrawable(R.drawable.selector_wallet_btn_black));
                        holder.tvEnter.setTextColor(UIUtils.getContext().getResources().getColor(R.color.colorWhite));
                        break;
                    case 2:
                        holder.tvOut.setBackgroundDrawable(UIUtils.getContext().getResources().getDrawable(R.drawable.shape_wallet_currency_no_use));
                        holder.tvOut.setTextColor(UIUtils.getContext().getResources().getColor(R.color.colorWhite));

                        holder.tvEnter.setBackgroundDrawable(UIUtils.getContext().getResources().getDrawable(R.drawable.selector_wallet_btn_black));
                        holder.tvEnter.setTextColor(UIUtils.getContext().getResources().getColor(R.color.colorWhite));
                        break;
                    case 3:
                        holder.tvOut.setBackgroundDrawable(UIUtils.getContext().getResources().getDrawable(R.drawable.selector_wallet_btn_yellow));
                        holder.tvOut.setTextColor(UIUtils.getContext().getResources().getColor(R.color.color_text1));

                        holder.tvEnter.setBackgroundDrawable(UIUtils.getContext().getResources().getDrawable(R.drawable.shape_wallet_currency_no_use));
                        holder.tvEnter.setTextColor(UIUtils.getContext().getResources().getColor(R.color.colorWhite));
                        break;
                }
                break;
        }

        holder.tvEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemListener != null) {
                    if (pos == 0) {
                        itemListener.OnWalletIntoClick(position);

                    } else {
                        // // 充值提现状态  9：不可以充值提现 1 ：可以充值 可以提现 2：仅可以充值 3：仅可以提现
                        switch (item.tokenStatus) {
                            case 9:
                            case 3:
                                BlocksToast.get().showToast(R.string.this_coin_not_support_recharge);
                                break;
                            case 1:
                            case 2:
                                itemListener.OnRechargeCoinClick(position);
                                break;
                        }
                    }
                }
            }
        });
        holder.tvOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemListener != null) {
                    // // 充值提现状态  9：不可以充值提现 1 ：可以充值 可以提现 2：仅可以充值 3：仅可以提现
                    switch (item.tokenStatus) {
                        case 9:
                        case 2:
                            BlocksToast.get().showToast(R.string.this_coin_not_support_writh);
                            break;
                        case 1:
                        case 3:
                            itemListener.OnEextractCoinClick(position);

                    }

                }
            }
        });
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new WalletCurrencyHolder(mInflater.inflate(R.layout.item_wallet_currency, parent, false));
    }

    static class WalletCurrencyHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_coin_logo)
        TextView tvCoinLogo;
        @BindView(R.id.tv_available_balance_label)
        TextView tvAvailableBalanceLabel;
        @BindView(R.id.tv_available_balance)
        TextView tvAvailableBalance;
        @BindView(R.id.tv_freeze_balance_label)
        TextView tvFreezeBalanceLabel;
        @BindView(R.id.tv_freeze_balance)
        TextView tvFreezeBalance;
        @BindView(R.id.tv_enter)
        TextView tvEnter;
        @BindView(R.id.tv_out)
        TextView tvOut;
        @BindView(R.id.tv_fee_reserve)
        TextView tvFeeReserve;
        @BindView(R.id.tv_fee_reserve_lable)
        TextView tvFeeReserveLable;

        public WalletCurrencyHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnItemListener itemListener;

    public void setOnItemListener(OnItemListener itemListener) {
        this.itemListener = itemListener;
    }

    public interface OnItemListener {

        void OnWalletIntoClick(int position);


        void OnRechargeCoinClick(int position);


        void OnEextractCoinClick(int position);
    }
}
